#!/usr/bin/env groovy
package com.yidao.www

def createGitChangeList(){
    def build = currentBuild
    def changeSets = build.changeSets
    def changeList = ''
    for(changeSet in changeSets){
        print 'changeSet: ' + changeSet
        for (changes in changeSet.items){
            print 'changes: ' + changes
            def changeFiles = ''
            if(changes.committer == 'changbin'){
                continue
            }
            for(file in changes.affectedFiles){
                changeFiles += file.path + '\n'
            }
            changeList += """===============================
更新日志:
${changes.comment.trim()}
提交人: ${changes.committer}
邮箱: ${changes.committerEmail}
提交时间: ${changes.committerTime}
commitID: ${changes.id}
修改文件:
${changeFiles}"""
        }
    }
    return changeList
}


def checkGitCommitFiles(){
    def base = new base()
    // def getFilesScript = "git show HEAD | grep diff | awk '{print \$4}'"
    def getFilesScript = "git diff HEAD~1 HEAD --diff-filter=ACMR --name-only"
    def files = base.scriptStdout(getFilesScript).trim()
    if(files != ''){
        def fileStr = files.replace('\n', ' ')
        println "fileStr: " + fileStr
        return fileStr
    }else{
        return null
    }
}

def checkCommitMessage(){
    def base = new base()
    def getCommitIDScript = "git rev-parse --short HEAD"
    def shortID = base.scriptStdout(getCommitIDScript).trim()
    def getCommitSubjectScript = "git log -1 --pretty=format:'%B'"
    def subject = base.scriptStdout(getCommitSubjectScript).trim()

    // def regSubCN = '(需求|缺陷)\\[#?\\d{5}\\]\\:[\\s\\S]*[\u4e00-\u9fa5][\\s\\S]*[\\n][\\s\\S]*\\[描述说明\\]\\:[\\s\\S]*'
    def regSubCN = '[\u4e00-\u9fa5][\\s\\S]*\\[#?\\d{5}\\]\\:?[\\s\\S]*[\u4e00-\u9fa5][\\s\\S]*[\\n][\\s\\S]*\\[描述说明\\]\\:?[\\s\\S]*'
    def regSubEN = '(REQ|DEF)\\[#?\\d{5}\\]\\:?[\\s\\S]*[\u4e00-\u9fa5][\\s\\S]*[\\n][\\s\\S]*\\[Description\\]\\:[\\s\\S]*'

    def templateCN = subject ==~ regSubCN
    def templateEN = subject ==~ regSubEN

    if(templateEN | templateCN){
        println '提交模板通过~'
    }else{
        println 'SUBJECT: \n' + subject
        throw new Exception('Git 提交模板异常,请验证后重新提交')
    }

}